<?php

namespace app\index\controller;

use EasyWeChat\Factory;
use think\App;
use think\Controller;
use think\Db;

class User extends Base
{
    public function __construct(App $app = null)
    {
        parent::__construct($app);
    }
    public function index(){
        if(session('user')){
            $user=\db('user')->find(session('user')['user_id']);
            $user['user_nickname']=base64_decode($user['user_nickname']);
            $this->assign('user',$user);
        }else{
            $this->assign('user',null);
        }
        return $this->fetch();
    }

    public function login(){
        $response = $this->wxbase->oauth->scopes(['scopes'   => 'snsapi_userinfo'])
            ->redirect('http://wnxlg.zytec.top/index/user/getuser')->send();
    }
    public function getUser(){
        $response = $this->wxbase->oauth->user()->toArray();
        $data=$response['original'];
        $user=[
            'user_openid'=>$data['openid'],
            'user_nickname'=>base64_encode($data['nickname']),
            'user_sex'=>$data['sex'],
            'user_headimg'=>$data['headimgurl'],
            'user_ctime'=>time(),
            ];
        $checkUser=db('user')->where('user_openid',$data['openid'])->find();
        if(!$checkUser){
            $saveUser=db('user')->insert($user);
            $newUser=\db('user')->where('user_openid',$data['openid'])->find();
            if($saveUser){
                session('user',$newUser);
                return $this->redirect('index');
            }else{
                return $this->error('获取失败','index');
            }
        }else{
            session('user',$checkUser);
            return $this->redirect('index');
        }
    }
    public function saveUserMsg(){
        if(request()->isAjax()){
            if($this->isLogin()==false){
                return json(['status'=>0,'msg'=>'请先登录']);
            }
            $userMsg=input('param.');
            $saveMsg= Db::name('user')->where('user_id',$this->getUserId())->update($userMsg);
            if($saveMsg){
                $user=\db('user')->find($this->getUserId());
                session('user',$user);
                return json(['status'=>1,'msg'=>'资料完善成功']);
            }else{
                return json(['status'=>0,'msg'=>'资料保存失败']);
            }
        }else{
            if($this->isLogin()){
                $user= \db('user')->where('user_id',$this->getUserId())->find();
            }else{
                $user=[
                    'user_name'=>"",
                    'user_phone'=>"",
                    'user_address'=>""
                ];
            }
            $this->assign('user',$user);
            return $this->fetch();
        }

    }
    public function tosq(){
        if(!session('user')){
            return['status'=>0,'msg'=>'请先登录'];
        }
        $userId=session('user')['user_id'];
        $chechMsg=\db('user')->find($userId);
        if(!$chechMsg['user_phone']){
            return['status'=>0,'msg'=>'请先完善资料后再来申请吧'];
        }
        $chechSq=\db('sq')->where('sq_user_id',$userId)->find();
        if(!$chechSq){
            $savesq=\db('sq')->insert(['sq_user_id'=>$userId]);
            if($savesq){
                return['status'=>1,'msg'=>'申请成功等待审核'];
            }else{
                return['status'=>0,'msg'=>'申请失败'];
            }
        }else{
            return['status'=>0,'msg'=>'已经存在申请记录，请勿重复申请'];
        }

    }
}
